<?php

/*
 * Explay Froms v. 0.1
 *
 * Copyright Golovdinov Alexander 2008
 * golovdinov@gmail.com
 *
 * License: GNU GPL v. 3
 *
 */

if (!defined ('EXPLAY')) exit;

class explayForm {
	
	protected static $out_list = '';
	private $title;
	private $action;
	protected static $method;
	private static $enctype;
	private $name;
	private $other_options = '';
	private $buttons = '';
	private static $hiddens = '';
	
	public function __construct ($title = '',$action = '', $method = 'get', $name = '') {
		$this->title = $title;
		$this->action = $action;
		self::$method = $method;
		$this->name = $name;
	}
	
	public function new_item ($type, $def_value = false) {
		if ($type == '') return false;
		
		switch ($type) {
			case 'text'     : return new textInput ($def_value); break;
			case 'textarea' : return new textarea ($def_value); break;
			case 'select'   : return new selectList ($def_value); break;
			case 'checkbox' : return new checkboxInput (); break;
			case 'radio'    : return new radioInput ($def_value); break;
			case 'password' : return new passwordInput ($def_value); break;
			case 'file'     : return new fileInput ($def_value); break;
			case 'hidden'   : return new hiddenInput ($def_value); break;
			default         : return false; break;
		}
	}
	
	public function set_title ($title) {
		$this->title = $title;
	}
	
	public function set_action ($url) {
		$this->action = $action;
	}
	
	public function set_method ($mthod) {
		if ($method != 'post' && $method != 'get') return false;	
		
		self::$method = $method;
	}
	
	protected static function set_enctype () {
		self::$enctype = "multipart/form-data";
	}
	
	public function set_name ($name) {
		$this->name = $name;
	}
	public function set_attr ($var, $value) {
		$this->other_options .= " $var=\"$value\"";
	}
	
	public function add_button ($type, $value, $name = '', $options = '') {
		if (($type != 'button' && $type != 'submit' && $type != 'image') || $value == '') return false;
		
		$options = trim ($options);
		if ($name != '') $name = " name=\"$name\" ";
		
		$this->buttons .= '<input type="'.$type.'" value="'.$value.'"'.$name.$options.' /> ';
	}
	
	protected static function add_item ($label, $input) {
		if ($label != '') {
			self::$out_list .= "\r\n\t\t<div class=\"row\">".
				"\r\n\t\t\t<div class=\"col_left\">".$label.":</div>".
				"\r\n\t\t\t<div class=\"col_right\">".$input."</div>".
				"\r\n\t\t</div>";
		} else {
			self::$out_list .= "\r\n\t\t<div class=\"row\">".
				"\r\n\t\t\t<div class=\"inrow\">".$input."</div>".
				"\r\n\t\t</div>";
		}
	}
	
	protected static function add_hidden ($input) {
		self::$hiddens .= "\r\n\t$input";
	}
	
	public function past ($print_option = false) {
		if ($this->title == '') return false;
		
		if ($this->name != '') $this->name = ' name="'.$this->name.'"';
		
		(self::$enctype != '') 
			? $enctype = ' enctype="'.self::$enctype.'"'
			: $enctype = '';
		
		$form = "\r\n<form class=\"explay_form\" action=\"".$this->action."\" method=\"".self::$method."\"".$enctype.$this->name.$this->other_options.">".
			self::$hiddens.
			"\r\n\t<div class=\"form_table\">".
			self::$out_list.
			"\r\n\t\t<div class=\"row\">".
			"\r\n\t\t\t<div class=\"inrow\">".
			( ($this->buttons == '') ? '<input type="submit" value="Send" />' : $this->buttons ).
			"</div>\r\n\t\t</div>".
			"\r\n\t</div>".
			"\r\n</form>";
			
		if ($print_option) return past_table ($this->title, $form, true);
		past_table ($this->title, $form);
	}
	
	
}

/*
 * TEXT
 */

class textInput extends explayForm {

	private $def_value;
	private $name = '';
	private $label = '';
	private $value = '';
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $pre_text = '';
	private $post_text = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	public function attr ($var, $value = false) {
		($value) 
			? $this->other_options .= "$var=\"$value\" "
			: $this->other_options .= $var.' ';
	}
	
	public function pre_text ($text) { $this->pre_text = $text; }
	public function post_text ($text) { $this->post_text = $text; }
	
	public function done () {
		
		if ($this->name == '') return false;
		
		if ($this->def_value && $this->value == '') {

			if (parent::$method == 'get') {
				if (isset($_GET[$this->name]))
					$this->value = safe_string_request ($_GET[$this->name]);
			} else {
				if (isset($_POST[$this->name]))
					$this->value = safe_string_request ($_POST[$this->name]);
			}
		}
		
		($this->error_message == '')
			? $input = $this->pre_text.'<input class="text" type="text" name="'.$this->name.'" value="'.$this->value.'" '.$this->other_options.'/>'.$this->help.$this->post_text
			: $input = $this->pre_text.'<span class="form_error"><input class="text" type="text" name="'.$this->name.'" value="'.$this->value.'" '.$this->other_options.'/><br />'.$this->error_message.'</span>'.$this->help.$this->post_text;
			
		parent::add_item ($this->label, $input);
	}

}


/*
 * PASSWORD
 */

class passwordInput extends explayForm {

	private $def_value;
	private $name = '';
	private $label = '';
	private $value = '';
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $pre_text = '';
	private $post_text = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	public function attr ($var, $value = false) {
		($value) 
			? $this->other_options .= "$var=\"$value\" "
			: $this->other_options .= $var.' ';
	}
	
	public function pre_text ($text) { $this->pre_text = $text; }
	public function post_text ($text) { $this->post_text = $text; }
	
	public function done () {
		
		if ($this->name == '') return false;
		
		if ($this->def_value && $this->value == '') {

			if (parent::$method == 'get') {
				if (isset($_GET[$this->name]))
					$this->value = safe_string_request ($_GET[$this->name]);
			} else {
				if (isset($_POST[$this->name]))
					$this->value = safe_string_request ($_POST[$this->name]);
			}
		}
		
		($this->error_message == '')
			? $input = $this->pre_text.'<input class="text" type="password" name="'.$this->name.'" value="'.$this->value.'" '.$this->other_options.'/>'.$this->post_text.$this->help
			: $input = $this->pre_text.'<span class="form_error"><input class="text" type="password" name="'.$this->name.'" value="'.$this->value.'" '.$this->other_options.'/>'.$this->post_text.'<br />'.$this->error_message.'</span>'.$this->help;
			
		parent::add_item ($this->label, $input);
	}

}



/*
 * FILE
 */

class fileInput extends explayForm {

	private $name = '';
	private $label = '';
	private $value = '';
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $pre_text = '';
	private $post_text = '';

	public function __construct () { }
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	
	public function pre_text ($text) { $this->pre_text = $text; }
	public function post_text ($text) { $this->post_text = $text; }
	
	public function done () {
		
		if ($this->name == '') return false;
		
			
		($this->error_message == '')
			? $input = $this->pre_text.'<input type="file" name="'.$this->name.'" />'.$this->post_text.$this->help
			: $input = $this->pre_text.'<span class="form_error"><input type="file" name="'.$this->name.'" />'.$this->post_text.'<br />'.$this->error_message.'</span>'.$this->help;
		
		parent::set_enctype ();
		parent::add_item ($this->label, $input);
	}

}


/*
 * HIDDEN
 */

class hiddenInput extends explayForm {

	private $def_value;
	private $name = '';
	private $value = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	
	public function done () {
		
		if ($this->def_value && $this->value == '') {

			if (parent::$method == 'get') {
				if (isset($_GET[$this->name]))
					$this->value = safe_string_request ($_GET[$this->name]);
			} else {
				if (isset($_POST[$this->name]))
					$this->value = safe_string_request ($_POST[$this->name]);
			}
		}
		
		if ($this->name == '') return false;
		$input = '<input type="hidden" name="'.$this->name.'" value="'.$this->value.'" />';
			
		parent::add_hidden ($input);
	}

}


/*
 * CHECKBOX
 */

class checkboxInput extends explayForm {

	private $name = '';
	private $label = '';
	private $value = '';
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $position = 'left';

	public function __construct () { }
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = ' value="'.$value.'"'; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	public function attr ($var, $value = false) {
		($value) 
			? $this->other_options .= "$var=\"$value\" "
			: $this->other_options .= $var.' ';
	}
	public function to_right () { $this->position = 'right'; }
	
	public function done () {
		
		if ($this->name == '' || $this->label == '') return false;
		
		($this->error_message == '')
			? $input = '<input type="checkbox" name="'.$this->name.'"'.$this->value.' '.$this->other_options.'/> '.$this->label.$this->help
			: $input = '<span class="form_error"><input type="checkbox" name="'.$this->name.' value="'.$this->value.'" '.$this->other_options.'/> '.$this->label.'<br />'.$this->error_message.'</span>'.$this->help;
			
		($this->position == 'left')
			? parent::add_item ('', $input)
			: parent::add_item ('&nbsp;', $input);
	}

}


/*
 * TEXTAREA
 */

class textarea extends explayForm {

	private $def_value;
	private $name = '';
	private $label = '';
	private $value = '';
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $pre_text = '';
	private $post_text = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	public function attr ($var, $value = false) {
		($value) 
			? $this->other_options .= "$var=\"$value\" "
			: $this->other_options .= ' '.$var;
	}
	
	public function pre_text ($text) { $this->pre_text = $text; }
	public function post_text ($text) { $this->post_text = $text; }
	
	public function done () {
		
		if ($this->name == '') return false;
		
		if ($this->def_value && $this->value == '') {

			if (parent::$method == 'get') {
				if (isset($_GET[$this->name]))
					$this->value = safe_string_request ($_GET[$this->name]);
			} else {
				if (isset($_POST[$this->name]))
					$this->value = safe_string_request ($_POST[$this->name]);
			}
		}
		
		if ($this->error_message == '')
			$input = $this->pre_text.'<textarea rows="10" name="'.$this->name.'"'.$this->other_options.'>'.$this->value.'</textarea>'.$this->post_text.$this->help;
		else
			$input = $this->pre_text.'<span class="form_error"><textarea rows="10" name="'.$this->name.'"'.$this->other_options.'>'.$this->value.'</textarea>'.$this->post_text.'<br />'.$this->error_message.'</span>'.$this->help;
			
		parent::add_item ($this->label, $input);
	}

}




/*
 * SELECT
 */

class selectList extends explayForm {

	private $def_value;
	private $name = '';
	private $label = '';
	private $value = '';
	private $opt_values = array ();
	private $opt_lables = array ();
	private $help = '';
	private $error_message = '';
	private $other_options = '';
	private $pre_text = '';
	private $post_text = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	public function help ($help) { $this->help = '<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	public function attr ($var, $value = false) {
		($value) 
			? $this->other_options .= "$var=\"$value\" "
			: $this->other_options .= ' '.$var;
	}
	
	public function pre_text ($text) { $this->pre_text = $text; }
	public function post_text ($text) { $this->post_text = $text; }	
	
	public function options_list ($values, $lables = '') {
		if ($values == '') return false;
		
		$values = explode (',', $values);
		($lables == '')
			? $lables = array ()
			: $lables = explode (',', $lables);
			
		foreach ($values as $value) {
			array_push ($this->opt_values, trim ($value));
		}
		
		if (count ($lables) > 0) {
			foreach ($lables as $label) {
				array_push ($this->opt_lables, trim ($label));
			}
		} else {
			$this->opt_lables = $this->opt_values;
		}
		
		if (count($this->opt_lables) != count($this->opt_values)) return false;
	}
	
	public function option ($value, $label = '', $selected = false) {
		if ($label == '') $label = $value;
		
		array_push ($this->opt_lables, $label);
		array_push ($this->opt_values, $value);
		
		if ($selected) $this->value = $value;
	}
	
	public function done () {
		
		if ($this->name == '') return false;
		
		$input = "\r\n\t\t\t\t<select name=\"".$this->name."\"".$this->other_options.">";
		
		$cnt = count ($this->opt_values);
		for ($i=0; $i<$cnt; $i++) {
			
			if ($this->def_value && $this->value == '') {
	
				if (parent::$method == 'get') {
					
					if (isset($_GET[$this->name]) && $_GET[$this->name] == $this->opt_values[$i])
						$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\" selected=\"selected\">".$this->opt_lables[$i]."</option>";
					else
						$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\">".$this->opt_lables[$i]."</option>";
				
				} else {
					
					if (isset($_POST[$this->name]) && $_POST[$this->name] == $this->opt_values[$i])
						$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\" selected=\"selected\">".$this->opt_lables[$i]."</option>";
					else
						$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\">".$this->opt_lables[$i]."</option>";
				
				}
			} else {
				
				if ($this->value == $this->opt_values[$i])
					$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\" selected=\"selected\">".$this->opt_lables[$i]."</option>";
				else
					$input .= "\r\n\t\t\t\t\t<option value=\"".$this->opt_values[$i]."\">".$this->opt_lables[$i]."</option>";
			}
		}
		
		$input .= "\r\n\t\t\t\t</select>".$this->post_text.$this->help."\r\n\t\t\t";
		
		if ($this->error_message != '') $input = '<span class="form_error">'.$input.'<br />'.$this->error_message.'</span>';
		
		parent::add_item ($this->label, $this->pre_text.$input);
	}

}



/*
 * RADIO
 */

class radioInput extends explayForm {

	private $def_value;
	private $name = '';
	private $label = '';
	private $value = '';
	private $opt_values = array ();
	private $opt_lables = array ();
	private $help = '';
	private $error_message = '';

	public function __construct ($def_value = false) {
		$this->def_value = $def_value;
	}
	
	public function label ($label) { $this->label = $label; }
	public function name ($name) { $this->name = $name; }
	public function value ($value) { $this->value = $value; }
	public function help ($help) { $this->help = "\r\n\t\t\t\t".'<br /><span class="form_help">'.$help.'</span>'; }
	public function error ($error_message) { $this->error_message = $error_message; }
	
	
	public function options_list ($values, $lables = '') {
		if ($values == '') return false;
		
		$values = explode (',', $values);
		($lables == '')
			? $lables = array ()
			: $lables = explode (',', $lables);
			
		foreach ($values as $value) {
			array_push ($this->opt_values, trim ($value));
		}
		
		if (count ($lables) > 0) {
			foreach ($lables as $label) {
				array_push ($this->opt_lables, trim ($label));
			}
		} else {
			$this->opt_lables = $this->opt_values;
		}
		
		if (count($this->opt_lables) != count($this->opt_values)) return false;
	}
	
	public function option ($value, $label = '', $selected = false) {
		if ($label == '') $label = $value;
		
		array_push ($this->opt_lables, $label);
		array_push ($this->opt_values, $value);
		
		if ($selected) $this->value = $value;
	}
	
	public function done () {
		
		if ($this->name == '') return false;
		
		$input = "";
		
		$cnt = count ($this->opt_values);
		for ($i=0; $i<$cnt; $i++) {
			
			if ($this->def_value && $this->value == '') {
	
				if (parent::$method == 'get') {
					
					if (isset($_GET[$this->name]) && $_GET[$this->name] == $this->opt_values[$i])
						$input .= "\r\n\t\t\t\t<input type=\"radio\" name=\"".$this->name."\" value=\"".$this->opt_values[$i]."\" checked=\"checked\"> ".$this->opt_lables[$i];
					else
						$input .= "\r\n\t\t\t\t<input value=\"".$this->opt_values[$i]."\"> ".$this->opt_lables[$i];
				
				} else {
					
					if (isset($_POST[$this->name]) && $_POST[$this->name] == $this->opt_values[$i])
						$input .= "\r\n\t\t\t\t<input type=\"radio\" name=\"".$this->name."\" value=\"".$this->opt_values[$i]."\" checked=\"checked\"> ".$this->opt_lables[$i];
					else
						$input .= "\r\n\t\t\t\t<input type=\"radio\" name=\"".$this->name."\" value=\"".$this->opt_values[$i]."\"> ".$this->opt_lables[$i];
				
				}
			} else {
				
				if ($this->value == $this->opt_values[$i])
					$input .= "\r\n\t\t\t\t<input type=\"radio\" name=\"".$this->name."\" value=\"".$this->opt_values[$i]."\" checked=\"checked\"> ".$this->opt_lables[$i];
				else
					$input .= "\r\n\t\t\t\t<input type=\"radio\" name=\"".$this->name."\" value=\"".$this->opt_values[$i]."\"> ".$this->opt_lables[$i];
			}
			
			if ($i+1 != $cnt) $input .= '<br />';
		}
		
		$input .= $this->help."\r\n\t\t\t\t";
		
		if ($this->error_message != '') $input = '<span class="form_error">'.$input.'<br />'.$this->error_message.'</span>';
		
		parent::add_item ($this->label, $input);
	}

}




function safe_string_request ($string) {
	$string = htmlspecialchars ($string, ENT_QUOTES);
	return $string;
}